package com.morder.net;

/**
 * Proxy's URL and port
 */
final class Apn {
  private String proxy = "";
  private int port = 0;

  public static boolean isValid(final Apn apn) {
    if(apn == null) {
      return false;
    }
    if((apn.proxy == null) || (apn.proxy.length() == 0) || (apn.port <= 0)) {
      return false;
    }
    return true;
  }

  public static boolean isUnValid(final Apn apn) {
    return !isValid(apn);
  }

  public Apn(String proxy, int port) {
    this.proxy = proxy;
    this.port = port;
  }

  public void setProxy(String proxy) {
    this.proxy = proxy;
  }

  public String getProxy() {
    return proxy;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public int getPort() {
    return port;
  }

  @Override
  public String toString() {
    return proxy + "," + port;
  }

  @Override
  public boolean equals(Object o) {
    if(o == this) {
      return true;
    }
    if(!(o instanceof Apn)) {
      return false;
    }
    Apn that = (Apn)o;
    if(that.port == this.port) {
      if((that.proxy == this.proxy) || ((that.proxy != null) && that.proxy.equals(this.proxy))) {
        return true;
      }
    }

    return false;
  }

  @Override
  public int hashCode() {
    int result = 17;
    result = (37*result) + port;
    result = (37*result) + proxy.hashCode();
    return result;
  }
}
